package com.rainng.coursesystem.service.admin;

import com.rainng.coursesystem.manager.admin.MemberEventManager;
import com.rainng.coursesystem.model.entity.EventEntity;
import com.rainng.coursesystem.model.entity.MemberEventEntity;
import com.rainng.coursesystem.model.vo.response.ResultVO;
import com.rainng.coursesystem.service.BaseService;
import org.springframework.stereotype.Service;

@Service
public class MemberEventService extends BaseService {
    private final MemberEventManager manager;

    public MemberEventService(MemberEventManager manager) {
        this.manager = manager;
    }

    public ResultVO getPageCount(String teamName, String eventName, String memberName) {
        return result(manager.getPageCount(teamName, eventName, memberName));
    }

    public ResultVO getPage(Integer index, String teamName, String eventName, String memberName) {
        return result(manager.getPage(index, teamName, eventName, memberName));
    }

    public ResultVO get(Integer id) {
        MemberEventEntity entity = manager.get(id);
        if (entity == null) {
            return failedResult("团员选项目Id: " + id + "不存在!");
        }

        return result(entity);
    }

    public ResultVO update(MemberEventEntity entity) {
        MemberEventEntity originEntity = manager.get(entity.getId());
        if (originEntity == null) {
            return failedResult("团员选项目Id: " + entity.getId() + "不存在!");
        }
        if (!originEntity.getEventId().equals(entity.getEventId())) {
            return failedResult("项目Id被篡改");
        }
        if (!originEntity.getMemberId().equals(entity.getMemberId())) {
            return failedResult("团员Id被篡改");
        }

        manager.update(entity);
        return result("更新成功");
    }

    public ResultVO delete(Integer id) {
        MemberEventEntity entity = manager.get(id);
        if (entity == null) {
            return failedResult("团员选项目Id: " + id + "不存在!");
        }

        manager.delete(entity);
        return result("删除成功");
    }

    public ResultVO create(MemberEventEntity entity) {
        if (manager.get(entity.getId()) != null) {
            return failedResult("团员选项目Id: " + entity.getId() + "已存在!");
        }
        if (manager.getMemberById(entity.getMemberId()) == null) {
            return failedResult("所属团员Id: " + entity.getMemberId() + "不存在!");
        }
        if (manager.getByEventIdAndMemberId(entity.getEventId(), entity.getMemberId()) != null) {
            return failedResult("团员已经选此项目");
        }
        EventEntity course = manager.getEventById(entity.getEventId());
        if (course == null) {
            return failedResult("所属项目Id: " + entity.getEventId() + "不存在!");
        }
        if (course.getSelectedCount() >= course.getMaxSize()) {
            return failedResult("活动容量已满");
        }
        if (!manager.inSameArea(entity.getEventId(), entity.getMemberId())) {
            return failedResult("" +
                    "项目与团员不在同一地区");
        }
        if (!course.getNumber().equals(manager.getMemberBudgetById(entity.getMemberId()))) {
            return failedResult("项目与团最大容量不匹配");
        }

        manager.create(entity);
        return result("添加成功");
    }
}
